<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('亚马逊地址设置')" />
	<th:block th:include="include :: list-css" />
	<style type="text/css">
		label.layui-form-label {width: 95px;padding-right: 0px;}
		.layui-form-radio {margin-top:0}
		.importTable{empty-cells:show;border-collapse:collapse;border-spacing:0;}
		.listext th{background:#eee;color:#3366cc;}
		.listext th,.listext td{border:solid 1px #ddd;text-align:left;padding:10px;font-size:14px;}

		#importProgress {z-index:20200202;background-color: #000;opacity: 0.3;display:none;}
		.importProgress {width:300px;z-index:20202222;position: fixed;top: 34%;left: 40%;display:none;}
		/* .layui-table-box{overflow-x: auto !important;}
		.layui-table-header{display: inline-block !important;} */
	</style>
</head>
<body>
<div class="main-content">
	<div class="content-right">
		<div class="search-top">
			<span>查询条件</span>
			<a href="javascript:;" layadmin-event="flexible" class="menuBtn">
				<i class="layui-icon layui-icon-spread-left" id="LAY_app_flexible" style="color: #fff;"></i>
			</a>
		</div>
		<div class="panel layout-expand layui-expand-east">
			<div class="panel-header">
				<div class="panel-tool" style="text-align: center; width: 26px;height: 30px;line-height: 30px;">
					<a href="javascript:;" layadmin-event="flexible" class="menuBtn">
						<i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible" style="color: #fff;"></i>
					</a>
				</div>
			</div>
			<div class="panel-body panel-title" style="text-align: center; width: 26px;">查<br>询<br>条<br>件</div>
		</div>
		<form class="layui-form">
			<th:block th:include="listinclude :: searchRight('page')" />
		</form>
	</div>
	<div class="mid-content">
		<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
	</div>
</div>
<!-- 表格头部工作栏-->
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<div class="layui-btn layui-table-tools importbtn-bg" lay-event="addbtn" shiro:hasPermission="hr:amazonAddress:add">
			<i class="dhicon dh-plus"></i> 新增
		</div>
		<div class="layui-btn layui-btn-danger layui-table-tools" onclick="remove()" shiro:hasPermission="hr:amazonAddress:delete">
			<p class="dhicon dh-remove" style="color:white;font-size:14px"><span class="fa_span">批量删除</span></p>
		</div>

	</div>
</script>
<!-- 表格操作区 -->
<script type="text/html" id="barDh">
	<a class="layui-btn layui-btn-xs dhicon dh-edit" lay-event="edit"  shiro:hasPermission="hr:amazonAddress:edit">&nbsp;编辑</a>
</script>


<th:block th:include="include :: footer" />
<th:block th:include="include :: businessJS" />
<th:block th:include="include :: searchJS" />
<th:block th:include="listinclude :: list-templeat" />
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "hr/amazonAddress";
	var gridColumns = [[${gridColumns}]],
			importFields =[[${importFields}]],
			moduleTable =[[${moduleTable}]];
	/*]]>*/
</script>
<script type="text/javascript">
	var element,thStr;
	var fieldWidth = {},		//导入列表字段宽度
			filedRequired = {},		//导入列表必填字段
			dateFiled = [],			//导入列表日期字段
			hasNull = false;		//导入数据必填字段有空值
	$.each(gridColumns.dictionaryForPageFieldList,function(i,v){
		fieldWidth[v.fields] = v.fieldFrontWidth
		if(v.fieldFrontRequired)filedRequired[v.fields] = v.fieldName;
		if(v.fieldFrontType == 't_date')dateFiled.push(v.fields);
	})
	var $,layer,laydate,form,table,wherestr={};
	layui.use(['table','laydate','form','layer','jquery','element'], function () {
		$ = layui.$, layer = layui.layer,laydate = layui.laydate , form = layui.form, table = layui.table, element = layui.element;
		serachRender();//查询条件初始渲染集合
		changeDropdown('fSl');//导入窗口——税率下拉可编辑赋事件
		$.tablelist.init({
			elem: '#dhtable'
			,modalName: "产品信息"
			,url: prefix + '/list'
			,updateUrl: prefix + "/edit?guid={id}"
			,initSort: {field: 'createTime',type: 'asc'}
			,totalRow: gridColumns.totalRow
			,cols: [gridColumns.gridBody]
			,toolbar: '#toolbar'
			,defaultToolbar: [{title:'列设置',layEvent: 'fieldSet',icon: 'layui-icon-set'}] //这里在右边显示
			,height:'full-65'
			,parseData: function(res){ //res 即为原始返回的数据
				var objpage=sessionStorage.getItem(moduleTable+'checkdata')
				if(objpage){
					objpage=JSON.parse(objpage)
					res.data.map((v,i)=>{
						objpage.map((n,m)=>{
							if(v.guid==n.guid){
						v.LAY_CHECKED=true
					}
				})
				})
				}
			}
		});
		//监听排序事件
		table.on('sort(ftable)', function(obj){
			$.tablelist.sort('dhtable',obj);
		});

		//监听行双击事件
		table.on('rowDouble(ftable)', function (obj) {
			top.layui.index.openTabsPage(prefix+ "/edit?guid="+obj.data.guid+'&type=0','亚马逊地址设置 - 详情')

		});

		// 多选赋值
		sessionStorage.setItem(moduleTable+'checkdata','')
		table.on('checkbox(ftable)', function(obj){
			var objpage=sessionStorage.getItem(moduleTable+'checkdata')
			if(obj.type=='all'){//判断是否全选
				var objdata=table.checkStatus('dhtable')
				objdata=objdata.data
				if(obj.checked){//判断是否选中
					if(objpage){
						objpage=JSON.parse(objpage)
						objpage=objpage.concat(objdata)
						objpage=newData(objpage)
					}else{
						objpage=objdata
					}
				}else{
					var objpage=JSON.parse(objpage)
					var deldata=table.cache.dhtable
					objpage=delData(objpage,deldata)
				}
				sessionStorage.setItem(moduleTable+'checkdata',JSON.stringify(objpage))
			}else{//点击复选框
				if(objpage){
					objpage=JSON.parse(objpage)
					if(obj.checked){
						objpage.push(obj.data)
					}else{
						objpage.map((v,i)=>{
							if(v.guid==obj.data.guid){
							objpage.splice(i,1)
						}
					})
					}
				}else{
					objpage=[]
					objpage.push(obj.data)
				}
				sessionStorage.setItem(moduleTable+'checkdata',JSON.stringify(objpage))
			}
		});

		//头工具栏事件
		table.on('toolbar(ftable)', function(obj){
			switch(obj.event){
				case 'addbtn':
					//openTabsPage跳转
					top.layui.index.openTabsPage(prefix+ "/add",'亚马逊地址设置 - 新增')
					break;
				case 'tableFull'://表格展开
					$('.north .layui-form').hide();
					table.reload('dhtable',{
						defaultToolbar: [{title:'收缩',layEvent:'tableShrink',icon:'layui-icon-screen-restore'}],
						height: $(window).height()-$('.north').height()-60<300?'300':$(window).height()-( $('.north').height()+$('.south').height()+12)
					});
					break;
				case 'tableShrink'://表格收缩
					$('.north .layui-form').show();
					table.reload('dhtable',{
						defaultToolbar: [{title:'展开',layEvent:'tableFull',icon:'layui-icon-screen-full'}],
						height: $(window).height()-$('.north').height()-60<300?'300':$(window).height()-( $('.north').height()+$('.south').height()+12)
					});
					break;
				case 'fieldSet':
					filedSet(moduleTable);
					break;
			};
		});

		//监听工具条
		table.on('tool(ftable)', function (obj) {
			var data = obj.data;
			switch(obj.event){
				case 'edit':
					top.layui.index.openTabsPage(prefix+ "/edit?guid="+obj.data.guid+'&type=1','亚马逊地址设置 - 编辑',function(index){
						var inputForm = $(window.frames["layui-layer-iframe" + index].document).contents().find(".layui-form");
						var is_return = 0,
								postStr = [],
								must_field={},
								Str = inputForm.serializeFormToJson();
						$.each(k_fields,function (fk,fv) {
							var fTitle = $(window.frames["layui-layer-iframe" + index].document).contents().find("#"+fv).attr("ftitle");
							must_field[fv] = fTitle + " 不能为空";
						})
						$.each(must_field,function (k,v) {
							if(!inputForm.find("#"+k).val()){
								layer.msg(v);
								is_return = 1;
								inputForm.find("#"+k).css('border','1px solid red');
								inputForm.find("#"+k).focus()
								return false;
							}
						})
						if(is_return)return false;
						postStr.push(Str);
						var data = {'infoItems':JSON.stringify(postStr)};
						$.operate.post('/hr/amazonAddress/editpost',data,function(result){
							allReload(result)
						});
					})
					break;
			};
		});
	});
	//通过guid彻底删除
	function remove(){
		var objpage=sessionStorage.getItem(moduleTable+'checkdata')
		if(!objpage){
			layer.msg("请先选择数据！");
		}else{
			$.modal.confirmCom('确定要删除么？',function(result){
				var infoItems=JSON.parse(objpage)
				$.operate.post(prefix + '/deletepost',{infoItems:JSON.stringify(infoItems)},function(result){
					setTimeout(function(){
						table.reload('dhtable', {page: {curr: 1},where:getWherestr()});
					}, 1000);
				});
			});
		}
	}

	// 筛选1个数组相同的数据
	function newData(arr) {
		const map = {};
		arr.forEach(item => {
			const obj = {};
		Object.keys(item).sort().map(key => obj[key] = item[key]);
		map[JSON.stringify(obj)] = item;
	});
		return Object.keys(map).map(key => JSON.parse(key));
	}

	// 筛选2个数组相同的数据
	function delData(data, deldata) {
		data.forEach(item => {
			deldata = deldata.filter(items => items.guid != item.guid);
	});
		return deldata;
	}

</script>
</body>
</html>